package com.foreveross.mofang.serviceconsole.core;

import java.util.Date;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.openkoala.koala.auth.core.domain.User;

/**
 * 实体基础类
 *  
 * @author fb
 */
@MappedSuperclass
public abstract class BaseEntity extends AbstractEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Column(name = "SN")   
	private String sn = UUID.randomUUID().toString();

	@Column(name = "CREATE_DATE")
	private Date createDate = new Date();  

	@Column(name = "LAST_UPDATE")
	private Date lastUpdate = new Date();

	@Column(name = "REMOVED")
	private Boolean removed = false;

	// 创建人
	@ManyToOne
	@JoinColumn(name = "CREATE_USER_ID")
	private User user;

	// 最后更新人
	@ManyToOne
	@JoinColumn(name = "LASTUPDATE_USER_ID")
	private User lastUpdateUser;

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;  
	}

	public Date getCreateDate() {
		return createDate; 
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getLastUpdate() {
		return lastUpdate;    
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}


	public Boolean getRemoved() {
		return removed;
	}

	public void setRemoved(Boolean removed) {
		this.removed = removed;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public User getLastUpdateUser() {
		return lastUpdateUser;
	}

	public void setLastUpdateUser(User lastUpdateUser) {
		this.lastUpdateUser = lastUpdateUser;
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(getCreateDate()).append(getSn()).append(getId())
				.toHashCode();
	}

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof BaseEntity)) {
			return false;
		}
		BaseEntity that = (BaseEntity) other;
		return new EqualsBuilder()
				.append(this.getCreateDate(), that.getCreateDate())
				.append(this.getSn(), that.getSn())
				.append(this.getId(), that.getId()).isEquals();
	}
}
